using Microsoft.AspNetCore.Mvc;
using Model.Utils;
using System;
using System.Linq;

namespace srb.net.Controllers.admin
{
    /// <summary>
    /// 简化实体生成控制器
    /// 使用 SQL 查询直接获取表结构，生成标准的实体类
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class SimpleEntityGeneratorController : ControllerBase
    {
        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <returns>连接测试结果</returns>
        [HttpGet("TestConnection")]
        public IActionResult TestConnection()
        {
            var result = SimpleEntityGenerator.TestConnection();
            
            if (result.Success)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }
        
        /// <summary>
        /// 生成 integral_grade 和 borrower_attach 表的实体类
        /// </summary>
        /// <returns>生成结果</returns>
        [HttpPost("GenerateCommonEntities")]
        public IActionResult GenerateCommonEntities()
        {
            // 定义常用的表名
            var commonTables = new string[]
            {
                "integral_grade",
                "borrower_attach",
                "borrower_info",
                "user_info",
                "user_account",
                "user_login_record",
                "user_bind",
                "user_integral",
                "user_integral_record",
                "lend_item",
                "lend_return",
                "lend_return_detail",
                "trans_flow",
                "dict",
                "dict_item"
            };
            
            var result = SimpleEntityGenerator.GenerateEntities(commonTables, "Model");
            
            if (result.Success)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }
        
        /// <summary>
        /// 生成指定表的实体类
        /// </summary>
        /// <param name="request">生成请求参数</param>
        /// <returns>生成结果</returns>
        [HttpPost("GenerateEntities")]
        public IActionResult GenerateEntities([FromBody] GenerateEntitiesRequest request)
        {
            if (request == null || request.TableNames == null || request.TableNames.Length == 0)
            {
                return BadRequest(new
                {
                    Success = false,
                    Message = "请指定要生成实体的表名"
                });
            }
            
            var result = SimpleEntityGenerator.GenerateEntities(
                request.TableNames,
                request.Namespace ?? "Model",
                request.OutputPath
            );
            
            if (result.Success)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }
        
        /// <summary>
        /// 生成单个表的实体类
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="namespaceName">命名空间名称</param>
        /// <returns>生成结果</returns>
        [HttpGet("GenerateSingleEntity")]
        public IActionResult GenerateSingleEntity(string tableName, string namespaceName = "Model")
        {
            if (string.IsNullOrEmpty(tableName))
            {
                return BadRequest(new
                {
                    Success = false,
                    Message = "请指定要生成实体的表名"
                });
            }
            
            var result = SimpleEntityGenerator.GenerateEntities(new[] { tableName }, namespaceName);
            
            if (result.Success)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }
        
        /// <summary>
        /// 批量生成指定表的实体类（通过URL参数）
        /// </summary>
        /// <param name="tableNames">表名，多个用逗号分隔</param>
        /// <param name="namespaceName">命名空间名称</param>
        /// <returns>生成结果</returns>
        [HttpGet("GenerateEntitiesByNames")]
        public IActionResult GenerateEntitiesByNames(string tableNames, string namespaceName = "Model")
        {
            if (string.IsNullOrEmpty(tableNames))
            {
                return BadRequest(new
                {
                    Success = false,
                    Message = "请指定要生成实体的表名，多个表名用逗号分隔"
                });
            }
            
            var tables = tableNames.Split(',', StringSplitOptions.RemoveEmptyEntries)
                .Select(t => t.Trim())
                .ToArray();
            
            var result = SimpleEntityGenerator.GenerateEntities(tables, namespaceName);
            
            if (result.Success)
            {
                return Ok(result);
            }
            else
            {
                return BadRequest(result);
            }
        }
    }
    
    /// <summary>
    /// 生成实体请求参数
    /// </summary>
    public class GenerateEntitiesRequest
    {
        /// <summary>
        /// 要生成实体的表名数组
        /// </summary>
        public string[] TableNames { get; set; }
        
        /// <summary>
        /// 命名空间名称
        /// </summary>
        public string Namespace { get; set; } = "Model";
        
        /// <summary>
        /// 输出路径
        /// </summary>
        public string OutputPath { get; set; }
    }
}
